package emergency;

import java.util.ArrayList;

import dispatch.DispatchCenter;

/**
 * A class for creating a new EmergencyBoard
 */
public class EmergencyBoard {

	/**
	 * Variable registering the emergencies on this EmergencyBoard
	 */
	private ArrayList<Emergency> emergencies;

	/**
	 * Variable registering the DispatchCenter of this EmergencyBoard
	 */
	private DispatchCenter dispatchcenter;

	/**
	 * Initialize this new EmergencyBoard
	 * 
	 * @param center
	 *            The DispatchCenter of this EmergencyBoard
	 * 
	 * @post A list with no emergencies is created
	 * 
	 * @post The DispatchCenter of this EmergencyBoard is set to the given
	 *       DispatchCenter
	 */
	public EmergencyBoard(DispatchCenter center) {
		setEmergencies(new ArrayList<Emergency>());
		setDispatchcenter(center);
	}

	/**
	 * Method for adding an Emergency on this EmergencyBoard
	 * 
	 * @param e
	 *            The Emergency to add on this EmergencyBoard
	 * 
	 * @post The given Emergency is added to this EmergencyBoard
	 * 
	 * @throws IllegalArgumentException
	 *             An EmergencyBoard can't be illegal
	 */
	public void addEmergency(Emergency e) {
		try {
			emergencies.add(e);
		} catch (Exception ex) {
			throw new IllegalArgumentException("Illegal emergency to add");
		}
	}

	/**
	 * Getter for DispatchCenter
	 * 
	 * @return Returns the DispatchCenter of this EmergencyBoard
	 */
	public DispatchCenter getDispatchcenter() {
		return dispatchcenter;
	}

	/**
	 * Getter for the Emergencies
	 * 
	 * @return Returns a cloned list with all the Emergencies on this EmergencyBoard
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Emergency> getEmergencies() {
		return (ArrayList<Emergency>) emergencies.clone();
	}

	/**
	 * Getter for the Emergencies cancelled
	 * 
	 * @return Returns a list with all the cancelled Emergencies
	 */
	public ArrayList<Emergency> getEmergenciesCancelled() {
		ArrayList<Emergency> emergencyList = new ArrayList<Emergency>();
		for (Emergency e : emergencies) {
			if (e.getStateName().equalsIgnoreCase("statecanceled"))
				emergencyList.add(e);
		}
		return emergencyList;
	}

	/**
	 * Getter for the Emergencies completed
	 * 
	 * @return Returns a list with all the completed Emergencies
	 */
	public ArrayList<Emergency> getEmergenciesCompleted() {
		ArrayList<Emergency> emergencyList = new ArrayList<Emergency>();
		for (Emergency e : emergencies) {
			if (e.getStateName().equalsIgnoreCase("statecompleted"))
				emergencyList.add(e);
		}
		return emergencyList;
	}

	/**
	 * Getter for the Emergencies of the given Type
	 * 
	 * @return Returns a list with all the Emergencies of the given Type on this
	 *         EmergencyBoard
	 */
	public ArrayList<Emergency> getEmergenciesFromType(String s) {
		ArrayList<Emergency> emergencyList = new ArrayList<Emergency>();
		for (Emergency e : emergencies) {
			if (e.getClass().toString()
					.equalsIgnoreCase("class Emergency." + s)) {
				emergencyList.add(e);
			}
		}
		return emergencyList;
	}

	/**
	 * Getter for the Emergencies in progress
	 * 
	 * @return Returns a list with all the Emergencies in progress
	 */
	public ArrayList<Emergency> getEmergenciesInProgress() {
		ArrayList<Emergency> emergencyList = new ArrayList<Emergency>();
		for (Emergency e : emergencies) {
			if (e.getStateName().equalsIgnoreCase("stateinprogress"))
				emergencyList.add(e);
		}
		return emergencyList;
	}

	/**
	 * Getter for the Emergencies recorded
	 * 
	 * @return Returns a list with all the recorded Emergencies
	 */
	public ArrayList<Emergency> getEmergenciesRecorded() {
		ArrayList<Emergency> emergencyList = new ArrayList<Emergency>();
		for (Emergency e : emergencies) {
			if (e.getStateName().equals("staterecorded"))
				emergencyList.add(e);
		}
		return emergencyList;
	}

	/**
	 * Setter for DispatchCenter
	 * 
	 * @param dispatchcenter
	 *            The DispatchCenter you want to assign to this EmergencyBoard
	 * 
	 * @post The DispatchCenter of this EmergencyBoard is set to the given
	 *       DispatchCenter
	 * 
	 * @throws IllegalArgumentException
	 *             , when the given DispatchCenter is null
	 * 
	 */
	private void setDispatchcenter(DispatchCenter dispatchcenter) {
		if (dispatchcenter != null)
			this.dispatchcenter = dispatchcenter;
		else
			throw new IllegalArgumentException(
					"The dispatchcenter of an emergencyboard cannot be null");
	}

	/**
	 * Setter for Emergencies
	 * 
	 * @param emergencies
	 *            The emergencies you want to assign to this EmergencyBoard
	 * 
	 * @post The emergencies of this EmergencyBoard are set to the given
	 *       emergencies
	 * 
	 * @throws IllegalArgumentException
	 *             when the given emergencylist is null
	 */
	private void setEmergencies(ArrayList<Emergency> emergencies) {
		if (emergencies != null)
			this.emergencies = emergencies;
		else
			throw new IllegalArgumentException(
					"The emergencies in an emergenyboard may be null");
	}
}
